#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    
    //父进程导入一个环境变量,新增，environ,可传可不传，因为已经加入到父进程的环境变量中
    //char* env_val = "MYVLA2=222222222222222222222222";
    //putenv(env_val);
    
    char* const myenv[]={
        "MYENY1=111111111",//都是字符串常量 会 warning
        "MYENY2=111111111",
        "MYENY3=111111111",
        "MYENY4=111111111",
        NULL
    };

    pid_t id = fork();
    if(id == 0)
    {
        //child
        printf("pid:%d, exec command begin\n",getpid());

        //execl("/usr/bin/ls","ls","-a","-l",NULL);

        execle("./mytest","mytest","-a","-b",NULL,myenv);//传自己定义的环境变量,不是新增，而是覆盖传递
        //execle("./mytest","mytest","-a","-b",NULL,environ);//char** environ == char* env[]
        
        //只替换新代码的数据和代码，环境变量不用传递。execl本身就可以传递环境变量
        execl("./mytest","mytest",NULL);//命令行怎么写，参数怎么传
    
        //成功不会向下执行，失败才会执行
        printf("pid:%d,exec command end",getpid());
        exit(1);//失败进程退出
    }
    else 
    {
        //father
        pid_t rid = waitpid(-1,NULL,0);

        if(rid > 0)
        {
            printf("wait success,rid = %d\n",rid);
        }

    }

    return 0;
}
